<?php

class Model_customer extends CI_Model{
	function __construct(){
        parent::__construct();
	}
	
	/*
			=================Function Add Customer=================
	*/
	
	function getDataCust($nama, $telp, $pt){
		$this->db->select("*");
		$this->db->from("customer");
		$this->db->join('pt','pt.pt_id = customer.customer_pt_id','left');
		$this->db->where("customer.customer_name", $nama);
		$this->db->where("customer.customer_telp", $telp);
		$this->db->where("pt.pt_name", $pt);
		$query = $this->db->get();
		if($query->num_rows() > 0){
			return true;
		}
		else{
			return false;	
		}
	}
	
	function check_perusahaan($nama){
		$this->db->select("pt_id");
		$this->db->from("pt");
		$this->db->where("pt_name",$nama);
		$query = $this->db->get();
		if($query->num_rows() > 0){
			$query2 = $query->result_array();
			$hasil = $query2[0]['pt_id'];
			return $hasil;
		}else{
			$data = array(
		   		'pt_name' => $nama
			);
			$this->db->insert('pt', $data); 
			return $this->db->insert_id('pt', $data);
		}
	}
	
	
	
	
	
	function addCustomer(){
		$id_perusahaan = "";
		if($this->input->post("perusahaan",true)!=""){
			$id_perusahaan  = $this->model_customer->check_perusahaan($this->input->post("perusahaan",true));		
																											
		}
		$data = array(
		   'customer_name' => $this->input->post("nama",true) ,
		   'customer_telp' => $this->input->post("no_telp",true) ,
		   'customer_pt_id' => $id_perusahaan,
		   'fax' => $this->input->post("fax",true),
		   'npwp' => $this->input->post("npwp",true) ,
		   'alamat' => $this->input->post("umur",true),
		   'alamat_1' => $this->input->post("alamat_1",true),
		   'alamat_2' => $this->input->post("alamat_2",true),
		   'rt' => $this->input->post("rt",true),
		   'rw' => $this->input->post("rw",true) ,
		   'kode_pos' => $this->input->post("kode_pos",true),
		   'kota' => $this->input->post("kota",true) ,
		   'provinsi' => $this->input->post("provinsi",true)
		  
	   
		);
	
		$this->db->insert('customer', $data); 
		$customerid =  $this->db->insert_id('pt', $data);
		$data = array(
		   'ship_name' => $this->input->post("nama_ship",true) ,
		   'ship_perusahaan' => $this->input->post("perusahaan_ship",true),
		   'ship_telp' => $this->input->post("no_telp_ship",true) ,
		   'ship_fax' => $this->input->post("Fax_ship",true),
		   'ship_alamat' => $this->input->post("alamat_ship",true) ,
		   'ship_alamat_1' => $this->input->post("alamat_ship_1",true) ,
		   'ship_alamat_2' => $this->input->post("alamat_ship_2",true) ,
		   'ship_rt' => $this->input->post("rt_ship",true),
		   'ship_rw' => $this->input->post("rw_ship",true) ,
		   'ship_kode' => $this->input->post("kode_ship",true),
		   'ship_kota' => $this->input->post("kota_ship",true) ,
		   'ship_provinsi' => $this->input->post("provinsi_ship",true),
		   'customerId' => $customerid
	   
		);
	
		$this->db->insert('customerdelivery', $data); 
		
		$data = array(
		   'bill_name' => $this->input->post("nama_ship",true) ,
		   'bill_perusahaan' => $this->input->post("perusahaan_ship",true),
		   'bill_telp' => $this->input->post("no_telp_ship",true) ,
		   'bill_fax' => $this->input->post("Fax_ship",true),
		   'bill_alamat' => $this->input->post("alamat_ship",true) ,
		   'bill_alamat_1' => $this->input->post("alamat_ship_1",true) ,
		   'bill_alamat_2' => $this->input->post("alamat_ship_2",true) ,
		   'bill_rt' => $this->input->post("rt_ship",true),
		   'bill_rw' => $this->input->post("rw_ship",true) ,
		   'bill_kode' => $this->input->post("kode_ship",true),
		   'bill_kota' => $this->input->post("kota_ship",true) ,
		   'bill_provinsi' => $this->input->post("provinsi_ship",true),
		   'customerId' => $customerid
	   
		);
	
		$this->db->insert('customerbill', $data); 
	}
	/*
			=================Function View Customer=================
	*/
	
	function getCustomerList(){
		$this->db->select("*");
		$this->db->from("customer");
		$this->db->join('pt','pt.pt_id = customer.customer_pt_id','left');
		$query = $this->db->get();
		if($query->num_rows() > 0){
			$data = array();
			$res = $query->result_array();
			foreach ($res as $hasil){
				$data[] = $hasil;

			}
			return $data;
		}else{
			return array();
		}
	}
	
	function ajaxGetCustomerList($ptId, $mode){
		$this->db->select("*");
		$this->db->from("customer");
		if($ptId!="X")
		{
			$this->db->where("customer_pt_id", $ptId);
		}
		$query = $this->db->get();
		if($query->num_rows() > 0){
			$data = array();
			$res = $query->result_array();
			foreach ($res as $hasil){
				$data[] = $hasil;

			}
			return $data;
		}else{
			return array();
		}
	}
	
	
	function getPtList(){
		$this->db->select("*");
		$this->db->from("pt");
		$this->db->order_by("pt_name asc");
		$query = $this->db->get();
		if($query->num_rows()>0){
			$hasil = $query->result_array();
			return $hasil;
		}
	}
	
	/*
			=================Function Edit Customer=================
	*/
	
	function getCustomerDet($id){
		$this->db->select("*");
		$this->db->from("customer");
		$this->db->join('pt','pt.pt_id = customer.customer_pt_id','left');
		$this->db->join('customerdelivery','customerdelivery.customerId = customer.customer_id','left');
		$this->db->where("customer_id",$id);
		$query = $this->db->get();
		if($query->num_rows() > 0){
			$hasil = $query->row_array();
			return $hasil;
		}else{
			return array();
		}
	}
	
	function editCustomer($customId){
		$id_perusahaan = "";
		if($this->input->post("perusahaan",true)!=""){
			$id_perusahaan  = $this->model_customer->check_perusahaan($this->input->post("perusahaan",true));		
																											
		}
		/*elseif(($ptId!="")&&($this->input->post("perusahaan",true)=="")){
			$id_perusahaan = "";
		}
		elseif(($ptId!="")&&($this->input->post("perusahaan",true)!="")){
			
		}*/
		$data = array(
		   'customer_name' => $this->input->post("nama",true) ,
		   'customer_telp' => $this->input->post("no_telp",true) ,
		   'customer_pt_id' => $id_perusahaan,
		   'fax' => $this->input->post("fax",true),
		   'npwp' => $this->input->post("npwp",true) ,
		   'alamat' => $this->input->post("alamat",true),
		   'alamat_1' => $this->input->post("alamat_1",true),
		   'alamat_2' => $this->input->post("alamat_2",true),
		   'rt' => $this->input->post("rt",true),
		   'rw' => $this->input->post("rw",true) ,
		   'kode_pos' => $this->input->post("kode_pos",true),
		   'kota' => $this->input->post("kota",true) ,
		   'provinsi' => $this->input->post("provinsi",true)
		  
	   
		);
		$this->db->where("customer_id",$customId);
		$this->db->update('customer', $data); 
		
		$data = array(
		   'ship_name' => $this->input->post("nama_ship",true) ,
		   'ship_perusahaan' => $this->input->post("perusahaan_ship",true),
		   'ship_telp' => $this->input->post("no_telp_ship",true) ,
		   'ship_fax' => $this->input->post("fax_ship",true),
		   'ship_alamat' => $this->input->post("alamat_ship",true) ,
		   'ship_alamat_1' => $this->input->post("alamat_ship_1",true) ,
		   'ship_alamat_2' => $this->input->post("alamat_ship_2",true) ,
		   'ship_rt' => $this->input->post("rt_ship",true),
		   'ship_rw' => $this->input->post("rw_ship",true) ,
		   'ship_kode' => $this->input->post("kode_ship",true),
		   'ship_kota' => $this->input->post("kota_ship",true) ,
		   'ship_provinsi' => $this->input->post("provinsi_ship",true)
		);
		$this->db->where("customerId",$customId);
		$this->db->update('customerdelivery', $data); 
		
	}
	
	/*
			=================Function Delete Customer=================
	*/
	function delDataCust($id){
		$this->db->where('customer_id', $id);
		$this->db->delete('customer'); 
	}
}